﻿using ConfExpert.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class KodIleArtikelAra : XPopupForm
    {
        public KodIleArtikelAra(int CustomerId)
        {
            InitializeComponent();
            this.CustomerId = CustomerId;
            if (CustomerId == -1)
            {
                cmbAll.EditValue = "Tüm kodlar içinde";
                cmbAll.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
            else
            {
                ((RepositoryItemComboBox)cmbAll.Edit).Items[1] = ((RepositoryItemComboBox)cmbAll.Edit).Items[1].ToString().Replace("[X]", AppSession.Lookups.V_Customer.FindByCustomerId(CustomerId).Name);
                cmbAll.EditValue = ((RepositoryItemComboBox)cmbAll.Edit).Items[1].ToStr();
            }

        }

        int CustomerId;
        public delegate void ArticleSelectedHandler(int ProductId);
        public event ArticleSelectedHandler ArticleSelected;

        private void Artikeller_Load(object sender, EventArgs e)
        {

            this.Text = "Kod Arama";

            gv.Init("FindSimilar3", string.Empty, 0, false);
            gv.SetCaptions("Score:Benzerlik", "KVendor:Kaynak Üretici", "KCustomer:Kaynak Müşteri", "tip:Tip", "Scan:Foto", "Name:Kod");

            gv.MakeRowSelectable();
            gv.MakeReadOnly();
            gv.Columns["Name"].OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;

            GridColumn col = gv.Columns["Score"];
            col.ColumnEdit = new RepositoryItemProgressBar();
            ((RepositoryItemProgressBar)col.ColumnEdit).ShowTitle = true;
            ((RepositoryItemProgressBar)col.ColumnEdit).PercentView = true;
            ((RepositoryItemProgressBar)col.ColumnEdit).Maximum = 100;

            ((RepositoryItemTextEdit)teKod.Edit).KeyUp += teKod_KeyUp;
            btnAra.ItemClick += btnAra_ItemClick;
            gv.BestFitColumns();
        }

        void btnAra_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            gv.RefreshDataSource(true, "FindSimilar3", teKod.EditValue, CustomerId, cmbAll.EditValue.ToStr() == "Tüm kodlar içinde");
            gv.BestFitColumns();
            this.Cursor = Cursors.Default;
        }

        void teKod_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
                btnAra.PerformClick();
        }

        public List<int> lst;
        void btnEkle_Click(object sender, System.EventArgs e)
        {
            gv.CloseEditor();
            gv.UpdateCurrentRow();

            lst = new List<int>();
            foreach (int rowHandle in gv.GetSelectedRows())
            {
                DataRow dr = gv.GetDataRow(rowHandle);
                lst.Add(dr["ProductId"].ToInt());
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gv_DoubleClick(object sender, EventArgs e)
        {
            if (ArticleSelected != null)
            {
                int productId = gv.GetFocusedRowCellValue("ProductId").ToInt(-1);
                if (productId > -1)
                    ArticleSelected(productId);
            }
        }
    }
}
